﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Article_management_system
{
    public partial class Articles : Form
    {
        public Articles()
        {
            InitializeComponent();
        }
        bool exit_Articles = true;//退出提示框处理使用
        private void Articles_FormClosing(object sender, FormClosingEventArgs e)//关闭窗体
        {
            if (exit_Articles)
            {
                var hint = MessageBox.Show($"是否退出系统！", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (!hint.Equals(DialogResult.OK))
                {
                    e.Cancel = true;
                }
                else
                {
                    System.Environment.Exit(0);//彻底关闭应用
                }
            }
        }

        private void Articles_Load(object sender, EventArgs e)//加载事件获取初始值
        {
                dataGridView1.DataSource = Sqldt.Refresh();
                textBox1.Focus();
        }

        private void button1_Click(object sender, EventArgs e)//查询按钮
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("查询内容不能为空！", "提示");
                dataGridView1.DataSource = Sqldt.Refresh();
                textBox1.Focus();
            }
            else
            {
                string sql = $"SELECT Id '编号',Title '文章标题',Author '文章作者',CreatedTime '创建时间'  FROM Articles where Author like '%{textBox1.Text}%'";
                dataGridView1.DataSource = Sqldt.Query(sql);
                textBox1.Focus();
                textBox1.SelectAll();
            }
        }

        private void button2_Click(object sender, EventArgs e)//刷新按钮
        {
            dataGridView1.DataSource = Sqldt.Refresh();
            textBox1.Focus();
        }

        private void button3_Click(object sender, EventArgs e)//添加按钮
        {
            Addmodification addmodification = new Addmodification();
            addmodification.ShowDialog();
            dataGridView1.DataSource = Sqldt.Refresh();
            textBox1.Focus();
        }

        private void button4_Click(object sender, EventArgs e)//修改按钮
        {

            if (dataGridView1.SelectedRows.Count > 0)
            {
                int Id = (int)dataGridView1.SelectedRows[0].Cells["编号"].Value;
                string Title = dataGridView1.SelectedRows[0].Cells["文章标题"].Value.ToString();
                string Author = dataGridView1.SelectedRows[0].Cells["文章作者"].Value.ToString();
                Addmodification addmodification = new Addmodification(Id, Title, Author);
                addmodification.ShowDialog();
                dataGridView1.DataSource = Sqldt.Refresh();
                textBox1.Focus();
            }
            else
            {
                MessageBox.Show("请选择一条数据！","提示");
            }
        }

        private void button5_Click(object sender, EventArgs e)//删除按钮
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var hint = MessageBox.Show($"是否删除该数据！", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (hint.Equals(DialogResult.OK))
                {
                    var Id = (int)dataGridView1.SelectedRows[0].Cells["编号"].Value;
                    var sql = $"delete from Articles where Id={Id}";
                    Sqldt.Judge(sql);
                    MessageBox.Show("删除成功！", "提示");
                    dataGridView1.DataSource = Sqldt.Refresh();
                    textBox1.Focus();
                }
                else
                {
                    dataGridView1.DataSource = Sqldt.Refresh();
                    textBox1.Focus();
                }

            }
            else
            {
                MessageBox.Show("请选择一条数据！", "提示");
            }
        }
    }
}
